/**
 * Created by xgjiang on 2015-10-10.
 */
define([
        'angular',
        'adConfig',
        'app/modules/dictionary/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.dictionary')
            .factory("adDictionaryService", ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();

                    var url = adConfig.baseServiceUrl + 'dictionary/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function addItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dictionary/add';
                    adRequest(url, {
                        dictionaryCode: row.dictionary_code,
                        dictionaryName: row.dictionary_name,
                        dictionaryValue: row.dictionary_value,
                        dictionaryType: row.dictionary_type,
                        dictionaryDesc: row.dictionary_desc
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function updateItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dictionary/update';
                    adRequest(url, {
                        dictionaryId: row.dictionary_id,
                        dictionaryCode: row.dictionary_code,
                        dictionaryName: row.dictionary_name,
                        dictionaryValue: row.dictionary_value,
                        dictionaryType: row.dictionary_type,
                        dictionaryDesc: row.dictionary_desc
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function deleteItems(rows) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dictionary/delete';
                    var dictionaryIds = _.map(rows, function (row) {
                        return row.dictionary_id;
                    });
                    adRequest(url, {
                        dictionaryIds: angular.toJson(dictionaryIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function getByType(type) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dictionary/getByType';

                    adRequest(url, {type: type}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function getByCode(code) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'dictionary/getByCode';

                    adRequest(url, {code: code}).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                return {
                    getPage: getPage,
                    addItem: addItem,
                    updateItem: updateItem,
                    deleteItems: deleteItems,
                    getByType: getByType,
                    getByCode: getByCode
                };
            }]);
    });